<template>
	<view class="container">
		<uni-row class="colcenter" :gutter="20" style="padding-top: 10px">
			<uni-col :span="5" class="xiangxia" style="background-color: white">
				<input class="uni-input" disabled="true" v-model="extractData" />
			</uni-col>
			<uni-col :span="10">
				<input class="uni-input" v-model="san" maxlength="3" type="number" placeholder="请输入号码" />
			</uni-col>
			<uni-col :span="5">
				<button class="buttonUi" size="mini" @click="huoqu">获取</button>
			</uni-col>
		</uni-row>
		<uni-row style="padding-top: 10px">
			<uni-col :span="24">
				<view class="da">
					<scroll-view :scroll-top="scrollTop" scroll-y="true" class="da">
						<uni-row>
							<uni-table ref="table" border stripe>
								<uni-tr>
									<uni-th width="30" align="center">序号</uni-th>
									<uni-th width="30" align="center">单式</uni-th>
									<uni-th width="30" align="center">频次</uni-th>
								</uni-tr>
								<uni-tr v-for="(item, index) in this.all">
									<uni-td align="center">{{ index + 1 }}</uni-td>
									<uni-td align="center">{{ item.name }}</uni-td>
									<uni-td align="center">{{ item.count }}</uni-td>
								</uni-tr>
							</uni-table>
						</uni-row>
					</scroll-view>
				</view>
			</uni-col>
		</uni-row>
		<uni-row class="colcenter" :gutter="20" style="padding-top: 10px">
			<uni-col :span="5"><view>起始编号 :</view></uni-col>
			<uni-col :span="8">
				<uni-easyinput class="xiao" v-model="firstNumber"></uni-easyinput>
			</uni-col>
			<uni-col :span="6">
				<button class="buttonUi" @click="extractValues" size="mini">提取</button>
			</uni-col>
		</uni-row>
		<uni-row class="colcenter" :gutter="20" style="top: 5px">
			<uni-col :span="5">结束编号 :</uni-col>
			<uni-col :span="8">
				<uni-easyinput class="xiao" type="text" v-model="secondNumber"></uni-easyinput>
			</uni-col>

			<uni-col :span="6">
				<button class="buttonUi" @click="copy" size="mini">复制</button>
			</uni-col>
		</uni-row>
		<view class="tu">
			<uni-card class="uni-body">
				<scroll-view style="height: 90px" class="scrollview dialogue-box" scroll-y="true" scroll-x="true">
					<text class="spliceAllText">{{ card }}</text>
				</scroll-view>
			</uni-card>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			scrollTop: 0,
			spliceAll: [], // 初始化spliceAll数组
			firstNumber: '',
			secondNumber: '',
			card: '',
			san: '',
			extractData: '',
			result: {},
			all: [],
			addArrIndex:[],
			mapIndex: {},
			allindex: [],
			old: {
				scrollTop: 0
			}
		};
	},
	onLoad(query) {
		if (query.args) {
			this.args = JSON.parse(query.args);
			this.getDataTotal(this.args.type, this.args.optionType);
		} else if (query.spliceAll) {
			// 解析参数中的spliceAll数组
			this.spliceAll = JSON.parse(query.spliceAll);
			this.calculateFrequency();
		}
	},
	methods: {
		getDataTotal(type, optionType) {
			// 获取号码统计
			const deviceId = uni.getStorageSync("phone");
			this.$myRequest({
				method: 'get',
				url: '/getDataTotal?type=' + type + '&optionType=' + optionType + '&deviceId=' + deviceId
			}).then((res) => {
				if (res.data) {
					this.spliceAll = res.data;
					this.calculateFrequency();
				}
			});
		},
		calculateFrequency() {
			let id = 1;
			let a = [0,1,2,3,4,5,6,7,8,9]
			a.forEach(firstValue => {
					a.forEach(secondValue => {
						a.forEach(thirdValue => {
							// 拼接成三位数并存储到数组中
							const concatenatedNumber = `${firstValue}${secondValue}${thirdValue}`;
							this.result[concatenatedNumber] = {
								count: 0,
								name: concatenatedNumber
							};
						})
					})
		 	})
			for (let i = 0; i < this.spliceAll.length; i++) {
				let item = this.spliceAll[i];
				if (this.result[item]) {
					this.result[item].count++;
				}
			}
			this.mapIndex = new Map()
			this.all = Object.values(this.result);
			this.all.sort((a, b) => b.count - a.count);
			this.all.forEach((res,index) =>{
				//this.addArrIndex.push(res.name)
				this.mapIndex.set(res.name,index)
			})
		},
		copy() {
			// 获取要复制的文本
			const textToCopy = this.card;
			if (textToCopy == '') {
				uni.showToast({
					title: '请先提取号码！',
					icon: 'none'
				});
				return;
			}
			// 复制功能
			uni.setClipboardData({
				data: textToCopy, // 要复制到剪贴板的文本
				success: () => {
					// 成功回调函数
					uni.showToast({
						title: '复制成功',
						icon: 'success'
					});
				},
				fail: () => {
					// 失败回调函数
					uni.showToast({
						title: '复制失败',
						icon: 'none'
					});
				}
			});
		},
		huoqu() {
			// 获取用户输入的单式内容

			let singleItem = this.san.trim();
			let index = -1;
			// 在 all 数组中查找对应的索引位置
			// for (let i = 0; i < this.all.length; i++) {
			// 	if (this.all[i].name === singleItem) {
			// 		index = i;
			// 		break; // 找到匹配项后退出循环
			// 	}
			// }
			// this.all.forEach((arr1,i) => {
			// 	if(arr1.name == singleItem){
			// 		index = i;
			// 		return; // 找到匹配项后退出循环
			// 	}
			// })
			//index = this.addArrIndex.indexOf(singleItem)
			// let map = new Map(this.addArrIndex.map((item, index) => [item, index]));
			index = this.mapIndex.get(singleItem)
			// 判断是否找到对应的数据
			if (index == -1) {
				uni.showToast({
					icon: 'none',
					title: '未找到对应的数据！'
				});
				return;
			}

			this.extractData = index + 1;
		},
		binarySearch(arr, target) {
		    let left = 0;
		    let right = arr.length - 1;
		 
		    while (left <= right) {
		        let mid = Math.floor((left + right) / 2);
		        if (arr[mid] === target) {
		            return mid; // 找到目标，返回索引
		        } else if (arr[mid] < target) {
		            left = mid + 1;
		        } else {
		            right = mid - 1;
		        }
		    }
		 
		    return -1; // 未找到目标，返回-1
		},
		extractValues() {
			let num1 = parseInt(this.firstNumber);
			let num2 = parseInt(this.secondNumber);
			// 判断输入是否合法
			if (isNaN(num1) || isNaN(num2)) {
				uni.showToast({
					icon: 'none',
					title: '请输入有效的起始编号'
				});
				return;
			}
			// 确保 num1 <= num2
			if (num1 > num2) {
				let temp = num1;
				num1 = num2;
				num2 = temp;
			}
			// 从spliceAll数组中提取数据
			let extractedData = [];
			if(num1 == 0){
				num1 = 1
			}
			for (let i = num1 - 1; i < num2 && i < this.all.length; i++) {
				extractedData.push(this.all[i].name);
			}
			//extractedData = this.all.slice(num1,num2)
			// 将提取的数据赋给uni-card的文本
			this.card = extractedData.join(' '); // 假设cardText是用来绑定uni-card中text的数据
		},
		goTop: function (e) {
			// 解决view层不同步的问题
			this.scrollTop = this.old.scrollTop;
			this.$nextTick(function () {
				this.scrollTop = 0;
			});
			uni.showToast({
				icon: 'none',
				title: '纵向滚动 scrollTop 值已被修改为 0'
			});
		}
	}
};
</script>
<style>
.container {
  padding: 10px;  
  background-color: #f6eee8;  
  border-radius: 10px;  
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1); 
}
.xiao {
	width: 100%;
}

.uni-body {
	width: 100%;
	height: 100px;
}
.kuang {
	border-radius: 10px;
	border: 1px solid gainsboro;
	height: 40px;
}
.kuang1 {
	border-radius: 10px;
	border: 1px solid gainsboro;
	width: 100px;
	height: 100px;
}
.tiqu {
	font-size: 10px;
	height: 20px;
	padding: 8px; /* 调整内边距 */
	text-align: center; /* 将文字居中对齐 */
	display: flex; /* 使用flex布局 */
	justify-content: center; /* 水平居中 */
	align-items: center; /* 垂直居中 */
}
.tiqu1 {
	font-size: 10px;
	height: 20px;
	padding: 8px; /* 调整内边距 */
	text-align: center; /* 将文字居中对齐 */
	display: flex; /* 使用flex布局 */
	justify-content: center; /* 水平居中 */
	align-items: center; /* 垂直居中 */
}
.wenbenkuang {
}
.xuhao {
	border: 1px solid gainsboro;
}
.colcenter {
	display: flex;
	justify-content: center; /* 列内水平居中 */
	align-items: center; /* 列内垂直居中 */
	height: 100%; /* 确保列高度100% */
}

.da {
	width: 100%;
	border: 1px solid gainsboro;
	display: flex;
	justify-content: center; /* 列内水平居中 */
	align-items: center; /* 列内垂直居中 */
	height: 420px; /* 确保列高度100% */
}
.tu {
	width: 100%;
	height: 100px;
	display: flex;
	height: 100%; /* 确保列高度100% */
}
.scroll-Y {
	height: 300rpx;
}

.scroll-view-item {
	height: 100rpx;
	line-height: 300rpx;
	text-align: center;
	font-size: 3rpx;
}
</style>
